#include "LogicConfig.h"
#include "GLGlobalInfoSerialize.h"
#include <map>
#include "CocosUtil.h"
#include "GLGlobalInfoSerialize.h"

CLogicConfig::CLogicConfig(void) : m_LogicIp(0), m_DefaultFixInfo(new GoodsFixInfo)
{
	m_BUpLevelLimit.resize(BattleSkillCount, 9999);
	m_LUpLevelLimit.resize(OtherSkillCount, 9999);
	m_BDownPropNeed.resize(BattleSkillCount, nullptr);
	m_AUpgradePropNeed.resize(CURRENT_ANIMAL_MAX_LEVEL + 1, nullptr);
}


CLogicConfig::~CLogicConfig(void)
{

}

bool CLogicConfig::Init()
{
	m_LogicIp = "192.168.0.160";
	m_LogicPort = 5555;

	for (int32_t i = 0; i != QuatityTypeCount; ++i)
	{
		this->AddSoulRate(1.00);
	}
	for (int32_t i = 0; i != QuatityTypeCount; ++i)
	{
		this->AddQualityRate(1.00);
	}

	if (!InitGoodsFixInfo())
		return false;
	if (!InitUpLimitBattle())
		return false;
	if (!InitUpLimitLife())
		return false;
	if (!InitDownNeedBattle())
		return false;
	if (!InitUpgradeNeedAll())
		return false;
	if (!InitDefaultBattle())
		return false;
	if (!InitDefaultLife())
		return false;

	return true;
}

bool CLogicConfig::InitGoodsFixInfo() {
	typedef int32_t	MKey_t;
	typedef std::string MValue_t;
	typedef std::map<MKey_t, MValue_t> MapIS_t;
	
	JsonMap tJson1("Config/goodsfix.json");
	MapIS_t tMap;
	tJson1.Parse<MKey_t, MValue_t, MapIS_t >(tMap);
	for (MapIS_t::const_iterator it = tMap.begin(); it != tMap.end(); ++it) {
		GoodsFixInfo tInfo;
		if (!CGLGlobalInfoSerialize::DecodeGoodsFixInfo(tInfo, it->second))
			return false;
		GoodsFixInfo* tInfoPtr = new GoodsFixInfo;
		*tInfoPtr = tInfo;
		m_GoodsFixInfo[it->first] = tInfoPtr;
	}

	return true;
}

bool CLogicConfig::InitUpLimitBattle() {
	JsonMap tJson1("Config/skillUpLimitBattle.json");
	CGLGlobalConfig::MapJson_t tMap;
	tJson1.Parse<CGLGlobalConfig::MapJsonKey_t, CGLGlobalConfig::MapJsonValue_t, CGLGlobalConfig::MapJson_t>(tMap);

	CGLGlobalConfig::InitUpLimitBattle(tMap, m_BUpLevelLimit);
	return true;
}

bool CLogicConfig::InitUpLimitLife() {
	JsonMap tJson1("Config/skillUpLimitLife.json");
	CGLGlobalConfig::MapJson_t tMap;
	tJson1.Parse<CGLGlobalConfig::MapJsonKey_t, CGLGlobalConfig::MapJsonValue_t, CGLGlobalConfig::MapJson_t>(tMap);

	CGLGlobalConfig::InitUpLimitLife(tMap, m_LUpLevelLimit);
	return true;
}

bool CLogicConfig::InitDownNeedBattle() {
	JsonMap tJson1("Config/skillDownNeedBattle.json");
	CGLGlobalConfig::MapJson_t tMap;
	tJson1.Parse<CGLGlobalConfig::MapJsonKey_t, CGLGlobalConfig::MapJsonValue_t, CGLGlobalConfig::MapJson_t>(tMap);

	CGLGlobalConfig::InitDownNeedBattle(tMap, m_BDownPropNeed);
	return true;
}

bool CLogicConfig::InitUpgradeNeedAll() {
	JsonMap tJson1("Config/skillUpgradeNeedAll.json");
	CGLGlobalConfig::MapJson_t tMap;
	tJson1.Parse<CGLGlobalConfig::MapJsonKey_t, CGLGlobalConfig::MapJsonValue_t, CGLGlobalConfig::MapJson_t>(tMap);

	CGLGlobalConfig::InitUpgradeNeedAll(tMap, m_AUpgradePropNeed);
	return true;
}

bool CLogicConfig::InitDefaultBattle() {
	JsonMap tJson1("Config/skillDefaultBattle.json");
	CGLGlobalConfig::MapJson_t tMap;
	tJson1.Parse<CGLGlobalConfig::MapJsonKey_t, CGLGlobalConfig::MapJsonValue_t, CGLGlobalConfig::MapJson_t>(tMap);

	CGLGlobalConfig::InitDefaultBattle(tMap, m_DefaultBSkill);
	return true;
}

bool CLogicConfig::InitDefaultLife() {
	JsonMap tJson1("Config/skillDefaultLife.json");
	CGLGlobalConfig::MapJson_t tMap;
	tJson1.Parse<CGLGlobalConfig::MapJsonKey_t, CGLGlobalConfig::MapJsonValue_t, CGLGlobalConfig::MapJson_t>(tMap);

	CGLGlobalConfig::InitDefaultLife(tMap, m_DefaultLSkill);
	return true;
}

Level_t CLogicConfig::GetBUpLevelLimit(int32_t model) const {
	return m_BUpLevelLimit[model];
}

Level_t CLogicConfig::GetLUpLevelLimit(int32_t model) const {
	return m_LUpLevelLimit[model];
}

const PropertyCondition* CLogicConfig::GetBDownPropNeed(int32_t model) const {
	return m_BDownPropNeed[model];
}

const PropertyCondition* CLogicConfig::GetAUpgradePropNeed(int32_t level) const {
	return m_AUpgradePropNeed[level];
}

Type_t CLogicConfig::GetDefaultBSkill(int32_t entityType, int32_t model) const {
	CGLGlobalConfig::MapDefaultSkill_t::const_iterator tIter = m_DefaultBSkill.find(entityType);
	if (tIter != m_DefaultBSkill.end()) {
		const CGLGlobalConfig::VecType_t& tTypes = tIter->second;
		if (model < tTypes.size())
			return tTypes[model];
	}

	return EntityTypeNone;
}

Type_t CLogicConfig::GetDefaultLSkill(int32_t entityType, int32_t model) const {
	CGLGlobalConfig::MapDefaultSkill_t::const_iterator tIter = m_DefaultLSkill.find(entityType);
	if (tIter != m_DefaultLSkill.end()) {
		const CGLGlobalConfig::VecType_t& tTypes = tIter->second;
		if (model < tTypes.size())
			return tTypes[model];
	}

	return EntityTypeNone;
}
